package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.inner.GroupTreeNode;
import com.augurit.common.base.extmodel.inner.OrgTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmGroup;
import com.augurit.common.entity.OmUserGroup;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.OmGroupForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.system.web.org.form.OmUserGroupForm;

public class OmGroupConverter {

	/**
	 * 将工作组Form对象的子机构列表、岗位列表和用户列表转换为ExtTreeNode列表
	 * @param form
	 * @return
	 */
	public static List<GroupTreeNode> convertChildListToTreeNodeList(OmGroupForm form){
		List<GroupTreeNode> result = new ArrayList();
		
		if(form != null){
			
			//转换子机构列表
			if(form.getChildren() != null && form.getChildren().size() > 0){
				for(OmGroupForm group : form.getChildren())
					result.add(convertToGroupTreeNode(group, null));
			}
			
			//转换岗位列表
			if(form.getPositions() != null && form.getPositions().size() > 0){
				for(OmPositionForm position : form.getPositions())
					result.add(OmPositionConverter.convertToGroupTreeNode(position));
			}
			
			//转换用户列表
			if(form.getUsers() != null && form.getUsers().size() > 0){
				for(OmUserForm user : form.getUsers())
					result.add(OmUserConverter.convertToGroupTreeNode(user));
			}
		}
		
		return result;
	}
	
	/**
	 * 将工作组Form对象列表转换为ExtTreeNode对象列表
	 * @param result
	 * @return
	 */
	public static List<GroupTreeNode> convertGroupToTreeNode(List<OmGroupForm> list, GroupTreeNode parentNode){
		List<GroupTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(OmGroupForm group : list)
				result.add(convertToGroupTreeNode(group, parentNode));
		}
		return result;
	}
	
	
	/**
	 * 将机构Form对象列表转换为ExtTreeNode对象列表
	 * @param list
	 * @return
	 */
	public static List<GroupTreeNode> convertOrgToTreeNode(List<OmOrgForm> list){
		List<GroupTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(OmOrgForm org: list)
				result.add(convertToGroupTreeNode(org));
		}
		return result;
	}
	
	/**
	 * 将岗位Form对象列表转换为ExtTreeNode对象列表
	 * @param list
	 * @return
	 */
	public static List<GroupTreeNode> convertPosToTreeNode(List<OmPositionForm> list, GroupTreeNode parentNode){
		List<GroupTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(OmPositionForm pos: list)
				result.add(convertToGroupTreeNode(pos, parentNode));
		}
		return result;
	}
	
	public static OmGroupForm convertToForm(OmGroup entity){
		OmGroupForm form = null;
		if(entity != null){
			form = new OmGroupForm();
			
			form.setGroupId(entity.getGroupId());
			form.setGroupName(entity.getGroupName());
			form.setOrgId(entity.getOrgId());
			form.setGroupLevel(entity.getGroupLevel());
			form.setGroupSeq(entity.getGroupSeq());
			form.setGroupType(entity.getGroupType());
			form.setParentGroupId(entity.getParentGroupId());
			form.setCreateTime(entity.getCreateTime());
			form.setCreator(entity.getCreator());
			form.setUpdateTime(entity.getUpdateTime());
			form.setUpdator(entity.getUpdator());
			form.setEndDate(entity.getEndDate());
			form.setStartDate(entity.getStartDate());
			form.setStatus(entity.getStatus());
			form.setGroupDesc(entity.getGroupDesc());
			form.setIsLeaf(entity.getIsLeaf());
			form.setSubCount(entity.getSubCount());
			form.setRemark(entity.getRemark());
			form.setLeaderUserId(entity.getLeaderUserId());
			
		}
		return form;
	}
	
	public static List<OmGroupForm> convertToForms(List<OmGroup> entities){
		List<OmGroupForm> formList = null;
		if(entities != null && entities.size() > 0){
			formList = new ArrayList<OmGroupForm>();
			
			for(OmGroup entity: entities){
				formList.add(convertToForm(entity));
			}
		}
		return formList;
	}
	
	
	/**
	 * 将用户Form对象列表转换为ExtTreeNode对象列表
	 * @param list
	 * @return
	 */
	public static List<GroupTreeNode> convertUserToTreeNode(List<OmUserForm> list){
		List<GroupTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(OmUserForm user: list)
				result.add(convertToGroupTreeNode(user));
		}
		return result;
	}
	
	
	/**
	 * 把OmOrgForm对象转换为GroupTreeNode结点
	 * @param form 机构form对象
	 * @return GroupTreeNode对象
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmOrgForm form){
		if(form != null){
			GroupTreeNode node = new GroupTreeNode();
			node.setOrgId(form.getOrgId());
			node.setPartyType(OrgConstant.PARTY_TYPE_ORG);
			node.setText(form.getOrgName());
			node.setIconCls(OrgTreeNode.CLS_ORG);
			node.setLeaf(form.getIsLeaf() != null && form.getIsLeaf().equals(OrgConstant.ONE) ? true : false);
			if(form.isChecked())
				node.setCanManager(true);
			else
				node.setCanManager(false);
			return node;
		}else
			return null;
	}
	

	/**
	 * 把OmGroupForm对象转换为GroupTreeNode结点
	 * @param form 工作组form对象
	 * @return GroupTreeNode对象
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmGroupForm form){
		if(form != null){
			GroupTreeNode node = new GroupTreeNode();
			node.setGroupId(form.getGroupId());
			node.setPartyType(OrgConstant.PARTY_TYPE_GROUP);
			node.setText(form.getGroupName());
			node.setIconCls(OrgTreeNode.CLS_WORKGROUP);
			node.setLeaf(form.getIsLeaf() != null && form.getIsLeaf().equals(OrgConstant.ONE) ? true : false);
			node.setCanManager(true);
			return node;
		}else
			return null;
	}
	
	/**
	 * 根据其父节点的把OmGroupForm对象转换为GroupTreeNode结点
	 * @param form 工作组form对象
	 * @param parentNode 要构造结点的父节点
	 * @return GroupTreeNode对象
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmGroupForm form, GroupTreeNode parentNode){
		if(form != null && parentNode != null){
			GroupTreeNode node = convertToGroupTreeNode(form);
			
			//要构造的结点父节点为机构结点，所以要构造的结点为顶级工作组
			if(parentNode.getPartyType().equals(OrgConstant.PARTY_TYPE_ORG)){
				//顶级工作组的隶属机构即为其父节点
				node.setParentOrgId(parentNode.getOrgId());
				node.setParentOrgName(parentNode.getText());
				//顶级工作组的父工作组为空
				node.setParentGroupId(null);
				node.setParentGroupName(null);
			}
			//要构造的结点父节点为工作组结点，所以要构造的结点为某个工作组的下级工作组
			else{
				//非顶级工作组的隶属机构继承它的父工作组的隶属机构
				node.setParentOrgId(parentNode.getParentOrgId());
				node.setParentOrgName(parentNode.getParentOrgName());
				//顶级工作组的父工作组即为及父节点
				node.setParentGroupId(parentNode.getGroupId());
				node.setParentGroupName(parentNode.getText());
				
			}
			//工作组结点没有岗位没有属性，设置为null
			node.setParentPosId(null);
			node.setParentPosName(null);
			return node;
		}else
			return null;
	}
	
	/**
	 * 把OmPositionForm对象转换为GroupTreeNode结点
	 * @param form 岗位form对象
	 * @return GroupTreeNode对象
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmPositionForm form){
		if(form != null){
			GroupTreeNode node = new GroupTreeNode();
			node.setPosId(form.getPosId());
			node.setPartyType(OrgConstant.PARTY_TYPE_POSITION);
			node.setText(form.getPosName());
			node.setIconCls(OrgTreeNode.CLS_POSITION);
			node.setLeaf(form.getIsLeaf() != null && form.getIsLeaf().equals(OrgConstant.ONE) ? true : false);
			node.setCanManager(true);
			return node;
		}else
			return null;
	}
	
	/**
	 * 根据其父节点的把OmPositionForm对象转换为GroupTreeNode结点
	 * @param form 工岗位form对象
	 * @param parentNode 要构造结点的父节点
	 * @return GroupTreeNode对象
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmPositionForm form, GroupTreeNode parentNode){
		if(form != null && parentNode != null){
			GroupTreeNode node = convertToGroupTreeNode(form);
			
			//要构造的结点父节点为工作组结点，所以要构造的结点为顶级岗位
			if(parentNode.getPartyType().equals(OrgConstant.PARTY_TYPE_GROUP)){
				//顶级岗位的隶属机构继承它所在的工作组的隶属机构
				node.setParentOrgId(parentNode.getParentOrgId());
				node.setParentOrgName(parentNode.getParentOrgName());
				//顶级岗位的隶属工作组即为及父节点
				node.setParentGroupId(parentNode.getGroupId());
				node.setParentGroupName(parentNode.getText());
				//顶级岗位的父岗位为null
				node.setParentPosId(null);
				node.setParentPosName(null);
			}
			//要构造的结点父节点为岗位结点，所以要构造的结点为某个岗位的下级岗位
			else{
				//继承父岗位的属性
				node.setParentOrgId(parentNode.getParentOrgId());
				node.setParentOrgName(parentNode.getParentOrgName());
				node.setParentGroupId(parentNode.getParentGroupId());
				node.setParentGroupName(parentNode.getParentGroupName());
				//其父岗位的即为父节点
				node.setParentPosId(parentNode.getParentPosId());
				node.setParentPosName(parentNode.getText());
			}
			return node;
		}else
			return null;
	}
	
	/**
	 * 将用户Form对象转换为ExtTreeNode对象列表
	 * @param form
	 * @return
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmUserForm form){
		if(form != null){
			GroupTreeNode node = new GroupTreeNode();
			node.setUserId(form.getUserId());
			node.setPartyType(OrgConstant.PARTY_TYPE_USER);
			node.setText(form.getUserName());
			if(form.getSex() != null){
				node.setIconCls(form.getSex().equals(OrgConstant.SEX_MALE)? OrgTreeNode.CLS_USER_MALE:OrgTreeNode.CLS_USER_FEMALE);
			}
			else{
				node.setIconCls(OrgTreeNode.CLS_USER_MALE);
			}
			node.setLeaf(true);
			node.setCanManager(true);
			return node;
		}else
			return null;
	}
	
	/**
	 * 把工作组entiy对象转换为工作组form对象
	 * @param enities 工作组entiy对象列表
	 * @param orgList 所有机构列表
	 * @param groupList 所有工作组列表
	 * @param groupTypeList 业务字典对象——工作组类型列表
	 * @param groupStatusList 业务字典对象——工作组状态列表
	 * @return 工作组form对象
	 */
	public static List<OmGroupForm> convertToFormList(List<OmGroup> enities, 
													  List<OmOrgForm> orgList, 
													  List<OmGroupForm> groupList,
													  List<SysCodeForm> groupTypeList,
													  List<SysCodeForm> groupStatusList){
		if(enities != null && enities.size() > 0){
			List<OmGroupForm> result = new ArrayList();
															
			for(OmGroup entity : enities){
				OmGroupForm form = convertToForm(entity);
				form.setOrgName(OrgUtils.getOrgName(form.getOrgId(),orgList));
				form.setParentGroupName(OrgUtils.getGroupName(form.getParentGroupId(), groupList));
				form.setGroupTypeLabel(OrgUtils.getItemName(form.getGroupType(), groupTypeList));
				form.setGroupStatusLabel(OrgUtils.getItemName(form.getStatus(), groupStatusList));
				result.add(form);
			}
			return result;
		}
		return null;
	}
	
	public static List<OmGroupForm> convertToFormList(List<OmGroup> list){
		return convertToFormList(list, null,null,null,null);
	}	
	
	public static void convertToEntity(OmGroupForm form, OmGroup entity){
		if(form != null && entity != null){

			if(form.getGroupId() != null)
				entity.setGroupId(form.getGroupId());
			
			if(form.getGroupName() != null && form.getGroupName().trim().length() > 0)
				entity.setGroupName(form.getGroupName());
			
			if(form.getOrgId() != null)
				entity.setOrgId(form.getOrgId());
			
			if(form.getParentGroupId() != null)
				entity.setParentGroupId(form.getParentGroupId());
			
			if(form.getGroupLevel() != null)
				entity.setGroupLevel(form.getGroupLevel());
			
			if(form.getGroupDesc() != null && form.getGroupDesc().trim().length() > 0)
				entity.setGroupDesc(form.getGroupDesc());
			
			if(form.getGroupType() != null && form.getGroupType().trim().length() > 0)
				entity.setGroupType(form.getGroupType());
			
			if(form.getGroupSeq() != null && form.getGroupSeq().trim().length() > 0)
				entity.setGroupSeq(form.getGroupSeq());
			
			if(form.getStartDate() != null)
				entity.setStartDate(form.getStartDate());
			
			if(form.getEndDate() != null)
				entity.setEndDate(form.getEndDate());
			
			if(form.getStatus() != null && form.getStatus().trim().length() > 0)
				entity.setStatus(form.getStatus());
			
			if(form.getLeaderUserId() != null)
				entity.setLeaderUserId(form.getLeaderUserId());
			
			if(form.getCreator() != null && form.getCreator().trim().length() > 0)
				entity.setCreator(form.getCreator());
			
			if(form.getCreateTime() != null)
				entity.setCreateTime(form.getCreateTime());
			
			if(form.getUpdator() != null && form.getUpdator().trim().length() > 0)
				entity.setUpdator(form.getUpdator());
			
			if(form.getUpdateTime() != null)
				entity.setUpdateTime(form.getUpdateTime());
			
			if(form.getIsLeaf() != null && form.getIsLeaf().trim().length() > 0)
				entity.setIsLeaf(form.getIsLeaf());
			
			if(form.getSubCount() != null)
				entity.setSubCount(form.getSubCount());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	
	public static void convertToEntity(OmUserGroupForm form, OmUserGroup entity){
		if(form != null && entity != null){

			if(form.getUgId() != null)
				entity.setUgId(form.getUgId());
			
			if(form.getGroupId() != null)
				entity.setGroupId(form.getGroupId());
			
			if(form.getUserId() != null)
				entity.setUserId(form.getUserId());
		}
	}
}
